package com.webservice;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.util.List;

/**
 * @author ytj
 * @date 2024/9/29
 */
public class WebServiceTest {
    public static void main(String[] args) throws Exception{
        doWebService();
    }

    public static String doWebService() throws Exception{
        //url去除wsdl(接口来源于网络)
        String url="http://www.dneonline.com/calculator.asmx";
        /**
         * Subtract通过wsdl文档中<wsdl:message> 和 <wsdl:part>获取
         * http://tempuri.org/为wsdl中targetNamespace值
         * intA和intB在wsdl文档对Subtract方法描述中
         * */
        String param= "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                "<soap:Body>" +
                "<Subtract xmlns=\"http://tempuri.org/\">" +
                "<intA>10</intA>" +
                "<intB>5</intB>" +
                "</Subtract>" +
                "</soap:Body>" +
                "</soap:Envelope>";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(url);
        //设置请求头
        httpPost.addHeader("Content-Type","text/xml; charset=utf-8");
        //获取wsdl中SOAPAction值
        httpPost.addHeader("SOAPAction", "http://tempuri.org/Subtract");
        httpPost.setEntity(new StringEntity(param));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        String result = EntityUtils.toString(response.getEntity());
        System.out.println("获取webservice返回值:"+result);
        String subtractResult = getResult(result, "SubtractResult");
        System.out.println("获取接口返回值:"+subtractResult);
        return subtractResult;
    }

    /**
     * 解析接口返回值(简易处理)
     * @param webserviceResult 接口返回值
     * @param resultName 返回值名称
     * @return
     * */
    public static String getResult(String webserviceResult,String resultName) throws Exception{
        Document document = DocumentHelper.parseText(webserviceResult);
        Element rootElement = document.getRootElement();
        return forElement(rootElement,resultName);
    }

    /**
     * 递归匹配resultName值
     * @param element
     * @param resultName
     * @return
     * */
    public static String forElement(Element element,String resultName){
        List<Element> elements = element.elements();
        if(CollectionUtil.isNotEmpty(elements)){
            for (Element childElement : elements) {
                if(childElement.getName().equals(resultName)){
                    return childElement.getText();
                }else{
                    return forElement(childElement,resultName);
                }
            }
        }
        return null;
    }
}
